﻿var txt = $('.writer').text();
var timeOut;
var txtLen = txt.length;
var theChar = 0;
$('.writer').text('|');
var instance;
function loadHandler(event) {
    // This is fired for each sound that is registered.
    instance = createjs.Sound.play("sound");  // play using id.  Could also use full sourcepath or event.src.
    instance.addEventListener("complete", createjs.proxy(this.handleComplete, this));
    instance.volume = 0.05;
   
    
}
function play(sound) {

    if (window.HTMLAudioElement) {


        createjs.Sound.addEventListener("fileload", createjs.proxy(loadHandler, this));
        createjs.Sound.registerSound("resources/css/sound/" + sound + '.mp3', "sound");
        
        setTimeout(function () {

            createjs.Sound.stop();
        }, 10000);

        //var snd = new Audio('');

        //if (snd.canPlayType('audio/mp3')) {
        //    snd = new Audio('css/sound/' + sound + '.mp3');
        //}

        //snd.play();
    }
    else {
        alert('HTML5 Audio is not supported by your browser!');
    }
}
function typeIt() {
   
    var humanize = Math.round(Math.random() * (30)) + 30;
    timeOut = setTimeout(function () {
        theChar++;
        var type = txt.substring(0, theChar);
        $('.writer').text(type + '|');
        typeIt();

        if (theChar == txtLen) {
            $('.writer').text($('.writer').text().slice(0, -1)); // remove the '|'
            clearTimeout(timeOut);
        }

    }, humanize);
};



$(function() {
    // Handler for .ready() called.
    play('joy');
    typeIt();

});
